package org.wotou.authority.system.quartz;


import org.apache.log4j.Logger;
import org.wotou.authority.domain.scheduleJobDO;
import org.wotou.authority.utils.SpringUtil;
import org.wotou.authority.utils.StringUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 通过配置DO调用程序
 *
 * @author zhangQ
 * @date 2017/12/6
 */
public class ScheduleTask {
    public final static Logger log = Logger.getLogger(ScheduleTask.class);

    /**
     * 通过反射调用scheduleJob中定义的方法
     *
     * @param scheduleJob
     */
    public static void invokMethod(scheduleJobDO scheduleJob) {
        Object object = null;
        Class jobClass = null;
        // springId不为空
        if (StringUtil.isNotNull(scheduleJob.getSpringId())) {
            object = SpringUtil.getBean(scheduleJob.getSpringId());
        }
        if (null == object) {
            log.error("任务名称 = [" + scheduleJob.getJobName() + "]---------------未启动成功，请检查是否配置正确！！！");
            return;
        }
        jobClass = object.getClass();
        Method method = null;
        try {
            method = jobClass.getDeclaredMethod(scheduleJob.getMethodName());
        } catch (NoSuchMethodException e) {
            log.error("任务名称 = [" + scheduleJob.getJobName() + "]---------------未启动成功，方法名设置错误！！！");
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (method != null) {
            try {
                method.invoke(object);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
